package com.gzhryc.shared_device.oem.micro.api.event;

import com.gzhryc.common.event.AbsDelayHandler;
import com.gzhryc.common.gps.GPSEntity;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.shared_device.oem.code.place.event.PlaceGPSEvent;
import com.gzhryc.shared_device.oem.micro.api.PlaceGPSServiceManager;

public class PlaceGPSEventHandler extends AbsDelayHandler<PlaceGPSEvent> {

    static Logger log = Logger.getLogger(PlaceGPSEventHandler.class);

    @Override
    public boolean execute(PlaceGPSEvent event) {
        try{
            if(event.getPlaceId() != null) {
                if (event.getType() == 1) {
                    if(event.getLatitude() != null && event.getLongitude() != null) {
                        //新增或修改
                        GPSEntity gpsEntity = PlaceGPSServiceManager.self().getDataService().getById(event.getPlaceId());
                        if (gpsEntity == null) {
                        	gpsEntity = new GPSEntity();
                            gpsEntity.setId(event.getPlaceId());
                        }
                        gpsEntity.setLatitude(event.getLatitude());
                        gpsEntity.setLongitude(event.getLongitude());

                        PlaceGPSServiceManager.self().getDataService().save(gpsEntity);
                    }
                } else if (event.getType() == 2) {
                    //删除
                    PlaceGPSServiceManager.self().getDataService().remove(event.getPlaceId());
                }
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return true;
    }
}
